﻿using System;
using System.Windows;
using System.Windows.Browser;

using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;

using Matchingo.Common.Constants;
using Matchingo.Common.Types.Enumerations;
using Matchingo.Common.Types.Interfaces;

namespace Matchingo.Modules.AboutScreenModule
{
    public class AboutScreenViewModel : DependencyObject, IMenuItem, IModule
    {
        #region ========== Private Properties ==========

        private IAboutScreenView View;

        private readonly IUnityContainer Container;
        private readonly IRegionManager RegionManager;
        private readonly IMenuFlowProvider FlowProvider;

        #endregion

        #region ========== Constructor ==========

        public AboutScreenViewModel(IUnityContainer container, IRegionManager regionManager, IMenuFlowProvider flowProvider)
        {
            Container = container;
            RegionManager = regionManager;
            FlowProvider = flowProvider;
        }

        #endregion

        #region ========== Public Methods ==========

        public void Initialize()
        {
            // get a handle on the View from the Container
            View = Container.Resolve<IAboutScreenView>();
            View.Initialize(this);

            // register this UserControl with our UI Flow Provider
            FlowProvider.RegisterUIModule(this, MenuModuleNames.ModuleNames.AboutMenu, View.RootElement);

            IRegion region = RegionManager.Regions[CommonConstants.SHELL_REGION_AboutScreen];
            region.Add(View);
        }

        public void VisitTwitterWebsite()
        {
            LaunchWebPage(CommonConstants.URL_TwitterPage);
        }

        public void VisitAuthorWebsite()
        {
            LaunchWebPage(CommonConstants.URL_AuthorPage);
        }

		public void VisitMichaelUrvanWebsite()
		{
			LaunchWebPage(CommonConstants.URL_MichaelUrvanPage);
		}
		
		public void VisitGameWebsite()
        {
            LaunchWebPage(CommonConstants.URL_GamePage);
        }

        public void VisitSilverlightWebsite()
        {
            LaunchWebPage(CommonConstants.URL_SilverlightPage);
        }

        public void VisitCodePlexWebsite()
        {
            LaunchWebPage(CommonConstants.URL_CodePlexPage);
        }

        public void VisitCreativeCommonsWebsite()
        {
            LaunchWebPage(CommonConstants.URL_CreativeCommonsPage);
        }

        public void VisitCrystalProjectWebsite()
        {
            LaunchWebPage(CommonConstants.URL_CrystalProjectPage);
        }

        public void CloseMenu()
        {
            FlowProvider.OpenMenu(MenuModuleNames.ModuleNames.TitleScreenMenu);
        }
        
        public bool TransitionMenu(InOutEnumeration.InOut inOrOut, DirectionNames.Direction direction, bool skipAnimation)
        {
            if (direction != DirectionNames.Direction.Down)
                return false;

            if (inOrOut == InOutEnumeration.InOut.In)
                View.ShowScreen(skipAnimation);
            else
                View.HideScreen(skipAnimation);

            return true;
        }
        
        #endregion

        #region ========== Private Methods ==========

        private static void LaunchWebPage(string url)
        {
            Uri sourceUri = new Uri(url);
            HtmlPage.Window.Navigate(sourceUri, "_blank");
        }

        #endregion
    }
}
